<template>
<!-- 上边距和左边距去掉，否则会导致打印时错位 -->
  <div class="app-container" style="margin-left: -20px;margin-top: -20px;" >
    <div id="printPreview"  v-html="mainPrint"></div>
  </div>
</template>

<script>

import { getPut, print } from '@/api/wire/print/print'

export default {
  name: 'printView',
  data() {
    return {
      //图片地址前缀
      imgurl: process.env.VUE_APP_BASE_API,
      // 打印预览内容
      mainPrint: undefined,
      // 遮罩层
      loading: true,
      // 选中用户组
      outParams: {},
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 状态数据字典
      statusOptions: [],
    };
  },
  created() {
    // const outParams = this.$route.params && this.$route.params.queryParams;
    let outParams = JSON.parse(this.$route.params.outParams);
    console.log(outParams);

    let queryParams;
    let putIds;
    let putList;
    if (outParams) {
      queryParams = outParams.queryParams;
      putIds = outParams.putIds;
      getPut(putIds).then(response => {
          putList = response.rows;
          let imgName; //图片名称
          let imgUrl;  //图片地址
          let topRightLabel; //右上角标签
          let bottomRightLabel = this.formatDate(queryParams.printdate); //右下角标签
          let bottomLabel; //底部标签
          let str = '<div style="width:205px;">';
          for (let i = 0; i < putList.length; i++){
            for (let j = 0; j < putList[i].printcount; j++){
              if (putList[i].patterns != ""){
                imgName = putList[i].patterns[0].patternName;    //获取图片名称
                imgUrl = this.imgurl + putList[i].patterns[0].patternImgUrl;   //获取图片地址
              }
              topRightLabel = queryParams.team + "-" + putList[i].furnace; //拼接右上角标签
              bottomLabel = putList[i].productName;
              str = str + '<div style="height:113.2px;border: 1px solid;border-color:white">' +
                '<div style="height:50%;margin-top:13px;font-size:20px;text-align:left;font-weight:bold;">' +
                '<div style="float:left;">'+imgName+'<img style="width:70px;height:50px" src="'+imgUrl+'"></div>' +
                '<div><label>'+ topRightLabel +'<br/>'+ bottomRightLabel +'</label></div>' +
                '</div>' +
                '<div style="height:50%;font-size:25px;text-align:center;margin-top:5px;font-weight:bold;">'+ bottomLabel +'</div></div>';
            }
          }
          str = str+"</div>";

          this.mainPrint = str;
        }
      );
    }

    clearTimeout(this.timer);  //清除延迟执行

    //打印延迟处理，等数据加载完毕后打印
    this.timer = setTimeout(()=>{   //设置延迟执行
      window.print();

      let formData = new FormData();
      formData.append("putIds", JSON.stringify(putIds));
      formData.append("queryParams", JSON.stringify(queryParams));

      print(formData).then(response => {
        this.$router.push({ path: "/wire/print" });
      });

    },1000);

  },
  methods: {
    formatDate(value) {
      let date = new Date(value);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? ('0' + MM) : MM;
      let d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      let h = date.getHours();
      h = h < 10 ? ('0' + h) : h;
      let m = date.getMinutes();
      m = m < 10 ? ('0' + m) : m;
      let s = date.getSeconds();
      s = s < 10 ? ('0' + s) : s;
      return MM + '-' + d ;
    },
  },
}
</script>

<style scoped>

</style>
